package cn.ibizlab.odoo.core.odoo_base.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ibase_automation;
import cn.ibizlab.odoo.core.odoo_base.domain.Base_automation;
import cn.ibizlab.odoo.core.odoo_base.filter.Base_automationSearchContext;
import cn.ibizlab.odoo.core.odoo_base.service.IBase_automationService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[自动动作] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Base_automationServiceImpl implements IBase_automationService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ibase_automationClientService base_automationClientService;

    @Override
    public Base_automation get(Integer id) {
        Ibase_automation clientModel = base_automationClientService.createModel();
        clientModel.setId(id);
        base_automationClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Base_automation get(Base_automation base_automation) {
        Base_automation retbase_automation = this.get(base_automation.getId()) ;
    	BeanCopier copier=BeanCopier.create(retbase_automation.getClass(), base_automation.getClass(), false);
        copier.copy(retbase_automation, base_automation, null);
        return base_automation ;
    }

    @Override
    public boolean create(Base_automation base_automation) {
        boolean bOk = false ;
        Ibase_automation clientModel = this.convert2Model(base_automation,null);
        base_automationClientService.create(clientModel) ;
        this.convert2Domain(clientModel,base_automation);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //base_automationClientService.removeBatch(base_automations) ;

    }

    @Override
    public void createBatch(List<Base_automation> base_automations, int batchSize) {

                        //base_automationClientService.createBatch(base_automations,batchSize) ;

    }

    @Override
    public boolean update(Base_automation base_automation) {
        boolean bOk = false ;
        Ibase_automation clientModel = this.convert2Model(base_automation,null);
        base_automationClientService.update(clientModel) ;
        this.convert2Domain(clientModel,base_automation) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ibase_automation clientModel = base_automationClientService.createModel();
        clientModel.setId(id);
        base_automationClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Base_automation> base_automations, int batchSize) {

        //base_automationClientService.updateBatch(base_automations,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Base_automation> searchDefault(Base_automationSearchContext context) {
        List<Base_automation> list = new ArrayList<Base_automation>();
        Page<cn.ibizlab.odoo.core.client.model.Ibase_automation> clientModelList = base_automationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ibase_automation clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Base_automation>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据资源工作时间查询
     */
    @Override
    public Page<Base_automation> selectByOdoo_trg_date_calendar(Integer id){
        Base_automationSearchContext context = new Base_automationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Base_automation> list = new ArrayList<Base_automation>();
        Page<cn.ibizlab.odoo.core.client.model.Ibase_automation> clientModelList = base_automationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ibase_automation clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Base_automation>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Base_automation> selectByOdoo_create(Integer id){
        Base_automationSearchContext context = new Base_automationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Base_automation> list = new ArrayList<Base_automation>();
        Page<cn.ibizlab.odoo.core.client.model.Ibase_automation> clientModelList = base_automationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ibase_automation clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Base_automation>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Base_automation> selectByOdoo_write(Integer id){
        Base_automationSearchContext context = new Base_automationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Base_automation> list = new ArrayList<Base_automation>();
        Page<cn.ibizlab.odoo.core.client.model.Ibase_automation> clientModelList = base_automationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ibase_automation clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Base_automation>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ibase_automation convert2Model(Base_automation domain , Ibase_automation model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = base_automationClientService.createModel() ;
        }

         if(domain.getBinding_model_idDirtyFlag())
            model.setBinding_model_id(domain.getBinding_model_id());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getFilter_pre_domainDirtyFlag())
            model.setFilter_pre_domain(domain.getFilter_pre_domain());
         if(domain.getOn_change_fieldsDirtyFlag())
            model.setOn_change_fields(domain.getOn_change_fields());
         if(domain.getActivity_summaryDirtyFlag())
            model.setActivity_summary(domain.getActivity_summary());
         if(domain.getActivity_user_field_nameDirtyFlag())
            model.setActivity_user_field_name(domain.getActivity_user_field_name());
         if(domain.getWebsite_publishedDirtyFlag())
            model.setWebsite_published(domain.getWebsite_published());
         if(domain.getUsageDirtyFlag())
            model.setUsage(domain.getUsage());
         if(domain.getCrud_model_nameDirtyFlag())
            model.setCrud_model_name(domain.getCrud_model_name());
         if(domain.getTrg_date_range_typeDirtyFlag())
            model.setTrg_date_range_type(domain.getTrg_date_range_type());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getFields_linesDirtyFlag())
            model.setFields_lines(domain.getFields_lines());
         if(domain.getActivity_noteDirtyFlag())
            model.setActivity_note(domain.getActivity_note());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getActivity_type_idDirtyFlag())
            model.setActivity_type_id(domain.getActivity_type_id());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getActivity_user_typeDirtyFlag())
            model.setActivity_user_type(domain.getActivity_user_type());
         if(domain.getWebsite_pathDirtyFlag())
            model.setWebsite_path(domain.getWebsite_path());
         if(domain.getPartner_idsDirtyFlag())
            model.setPartner_ids(domain.getPartner_ids());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getTrg_date_idDirtyFlag())
            model.setTrg_date_id(domain.getTrg_date_id());
         if(domain.getXml_idDirtyFlag())
            model.setXml_id(domain.getXml_id());
         if(domain.getLast_runDirtyFlag())
            model.setLast_run(domain.getLast_run());
         if(domain.getTemplate_idDirtyFlag())
            model.setTemplate_id(domain.getTemplate_id());
         if(domain.getBinding_typeDirtyFlag())
            model.setBinding_type(domain.getBinding_type());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getModel_nameDirtyFlag())
            model.setModel_name(domain.getModel_name());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getChannel_idsDirtyFlag())
            model.setChannel_ids(domain.getChannel_ids());
         if(domain.getModel_idDirtyFlag())
            model.setModel_id(domain.getModel_id());
         if(domain.getHelpDirtyFlag())
            model.setHelp(domain.getHelp());
         if(domain.getWebsite_urlDirtyFlag())
            model.setWebsite_url(domain.getWebsite_url());
         if(domain.getActivity_user_idDirtyFlag())
            model.setActivity_user_id(domain.getActivity_user_id());
         if(domain.getActivity_date_deadline_rangeDirtyFlag())
            model.setActivity_date_deadline_range(domain.getActivity_date_deadline_range());
         if(domain.getTrg_date_rangeDirtyFlag())
            model.setTrg_date_range(domain.getTrg_date_range());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getFilter_domainDirtyFlag())
            model.setFilter_domain(domain.getFilter_domain());
         if(domain.getActivity_date_deadline_range_typeDirtyFlag())
            model.setActivity_date_deadline_range_type(domain.getActivity_date_deadline_range_type());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getChild_idsDirtyFlag())
            model.setChild_ids(domain.getChild_ids());
         if(domain.getLink_field_idDirtyFlag())
            model.setLink_field_id(domain.getLink_field_id());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getTriggerDirtyFlag())
            model.setTrigger(domain.getTrigger());
         if(domain.getAction_server_idDirtyFlag())
            model.setAction_server_id(domain.getAction_server_id());
         if(domain.getCrud_model_idDirtyFlag())
            model.setCrud_model_id(domain.getCrud_model_id());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getTrg_date_calendar_id_textDirtyFlag())
            model.setTrg_date_calendar_id_text(domain.getTrg_date_calendar_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getTrg_date_calendar_idDirtyFlag())
            model.setTrg_date_calendar_id(domain.getTrg_date_calendar_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Base_automation convert2Domain( Ibase_automation model ,Base_automation domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Base_automation();
        }

        if(model.getBinding_model_idDirtyFlag())
            domain.setBinding_model_id(model.getBinding_model_id());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getFilter_pre_domainDirtyFlag())
            domain.setFilter_pre_domain(model.getFilter_pre_domain());
        if(model.getOn_change_fieldsDirtyFlag())
            domain.setOn_change_fields(model.getOn_change_fields());
        if(model.getActivity_summaryDirtyFlag())
            domain.setActivity_summary(model.getActivity_summary());
        if(model.getActivity_user_field_nameDirtyFlag())
            domain.setActivity_user_field_name(model.getActivity_user_field_name());
        if(model.getWebsite_publishedDirtyFlag())
            domain.setWebsite_published(model.getWebsite_published());
        if(model.getUsageDirtyFlag())
            domain.setUsage(model.getUsage());
        if(model.getCrud_model_nameDirtyFlag())
            domain.setCrud_model_name(model.getCrud_model_name());
        if(model.getTrg_date_range_typeDirtyFlag())
            domain.setTrg_date_range_type(model.getTrg_date_range_type());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getFields_linesDirtyFlag())
            domain.setFields_lines(model.getFields_lines());
        if(model.getActivity_noteDirtyFlag())
            domain.setActivity_note(model.getActivity_note());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getActivity_type_idDirtyFlag())
            domain.setActivity_type_id(model.getActivity_type_id());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getActivity_user_typeDirtyFlag())
            domain.setActivity_user_type(model.getActivity_user_type());
        if(model.getWebsite_pathDirtyFlag())
            domain.setWebsite_path(model.getWebsite_path());
        if(model.getPartner_idsDirtyFlag())
            domain.setPartner_ids(model.getPartner_ids());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getTrg_date_idDirtyFlag())
            domain.setTrg_date_id(model.getTrg_date_id());
        if(model.getXml_idDirtyFlag())
            domain.setXml_id(model.getXml_id());
        if(model.getLast_runDirtyFlag())
            domain.setLast_run(model.getLast_run());
        if(model.getTemplate_idDirtyFlag())
            domain.setTemplate_id(model.getTemplate_id());
        if(model.getBinding_typeDirtyFlag())
            domain.setBinding_type(model.getBinding_type());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getModel_nameDirtyFlag())
            domain.setModel_name(model.getModel_name());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getChannel_idsDirtyFlag())
            domain.setChannel_ids(model.getChannel_ids());
        if(model.getModel_idDirtyFlag())
            domain.setModel_id(model.getModel_id());
        if(model.getHelpDirtyFlag())
            domain.setHelp(model.getHelp());
        if(model.getWebsite_urlDirtyFlag())
            domain.setWebsite_url(model.getWebsite_url());
        if(model.getActivity_user_idDirtyFlag())
            domain.setActivity_user_id(model.getActivity_user_id());
        if(model.getActivity_date_deadline_rangeDirtyFlag())
            domain.setActivity_date_deadline_range(model.getActivity_date_deadline_range());
        if(model.getTrg_date_rangeDirtyFlag())
            domain.setTrg_date_range(model.getTrg_date_range());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getFilter_domainDirtyFlag())
            domain.setFilter_domain(model.getFilter_domain());
        if(model.getActivity_date_deadline_range_typeDirtyFlag())
            domain.setActivity_date_deadline_range_type(model.getActivity_date_deadline_range_type());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getChild_idsDirtyFlag())
            domain.setChild_ids(model.getChild_ids());
        if(model.getLink_field_idDirtyFlag())
            domain.setLink_field_id(model.getLink_field_id());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getTriggerDirtyFlag())
            domain.setTrigger(model.getTrigger());
        if(model.getAction_server_idDirtyFlag())
            domain.setAction_server_id(model.getAction_server_id());
        if(model.getCrud_model_idDirtyFlag())
            domain.setCrud_model_id(model.getCrud_model_id());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getTrg_date_calendar_id_textDirtyFlag())
            domain.setTrg_date_calendar_id_text(model.getTrg_date_calendar_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getTrg_date_calendar_idDirtyFlag())
            domain.setTrg_date_calendar_id(model.getTrg_date_calendar_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
